<template>
<div>
    <el-card style="padding:10px;background:#EBEEF5">
    <div style="margin: 10px 0;width: 100%">
      <el-button type="primary" :icon="CirclePlus" @click="add">新增</el-button>
      <el-button type="danger" :icon="Remove" @click="Show" v-if="!isShow">批量删除</el-button>
      <el-button type="danger" :icon="Remove" @click="deleteBatch" v-if="isShow">删除</el-button>
      <el-button type="danger" :icon="CircleClose"  @click="Show" v-if="isShow">取消</el-button>
      <el-upload     :action="'http://localhost:8086/qbank/import'"   :on-success="handleUploadSuccess"   :show-file-list=true     accept="xlsx"   style="display: inline-block; margin: 0 10px" >
        <el-button type="primary" :icon="Download">导入</el-button>
      </el-upload>
      <el-button :icon="Upload"  type="primary" @click="exportUser">导出</el-button>

      <el-input v-model="garbageName" placeholder="请输入垃圾名称" style="margin-left:10px;width: 15%;" clearable/>
      <el-select clearable v-model="garbageType" placeholder="请选择垃圾类别" style="margin-left:10px;width: 15%;">
        <el-option v-for="item in types" :key="item.typename" :label="item.typename" :value="item.typeid"></el-option>
      </el-select>
      <el-button type="primary" :icon="Search" style="margin-left: 10px" @click="load">查询</el-button>
      <el-button type="primary" :icon="Refresh" style="margin-left: 10px" @click="reset">重置</el-button>

    </div>
    <el-table :data="tableData"  :row-style="tableRowStyle" :cell-style="{'text-align':'center'}" :header-cell-style="{'text-align':'center'}"
              border resizable="true" style="width: 100%" @selection-change="handleSelectionChange">
      <el-table-column v-if="isShow" type="selection" width="55"></el-table-column>
      <el-table-column prop="questionId" label="ID" sortable/>
      <el-table-column prop="garbageName" label="垃圾名称"/>
      <el-table-column label="垃圾类型" width="180">
        <template #default="scope">
          <div>
            <span style="margin-left: 10px" v-if="scope.row.garbageType == 1">干垃圾</span>
            <span style="margin-left: 10px" v-if="scope.row.garbageType == 2">湿垃圾</span>
            <span style="margin-left: 10px" v-if="scope.row.garbageType == 3">有害垃圾</span>
            <span style="margin-left: 10px" v-if="scope.row.garbageType == 4">可回收垃圾</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="remark" label="备注"/>
      <el-table-column fixed="right" label="操作" width="300px">
        <template #default="scope">
          <el-button type="primary" :icon="Edit" @click="handleEdit(scope.row)">编辑
          </el-button>
          <el-popconfirm title="确定要删除吗?" @confirm="handleDelete(scope.row.id)">
            <template #reference>
              <el-button :icon="Remove" type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div style="margin: 10px 0">
<!--      <el-dialog title="用户拥有的图书列表" v-model="bookVis" width="30%">-->
<!--        <el-table :data="bookList" stripe border>-->
<!--          <el-table-column prop="id" label="ID"></el-table-column>-->
<!--          <el-table-column prop="name" label="名称"></el-table-column>-->
<!--          <el-table-column prop="price" label="价格"></el-table-column>-->
<!--        </el-table>-->
<!--      </el-dialog>-->
      <el-dialog
          v-model="dialogVisible"
          title="提示"
          width="30%"
      >
        <el-form label-width="120px">
          <el-form-item :model="form" label="垃圾名称">
            <el-input v-model="form.garbageName" style="width: 80%"></el-input>
          </el-form-item>
          <el-form-item :model="form" label="垃圾类别">
            <el-select clearable v-model="form.garbageType" placeholder="请选择类别" style="width: 100%">
              <el-option v-for="item in types" :key="item.typename" :label="item.typename" :value="item.typeid"></el-option>
            </el-select>
          </el-form-item>
            <el-form-item :model="form" label="垃圾分析">
              <el-input v-model="form.analysis" style="width: 80%"></el-input>
            </el-form-item>
              <el-form-item :model="form" label="垃圾备注">
                <el-input v-model="form.remark" style="width: 80%"></el-input>
              </el-form-item>
        </el-form>
        <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="save">确定</el-button>
      </span>
        </template>
      </el-dialog>
    </div>
  </el-card>
  <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[2,5,10,20]"
      :page-size="pageSize"
      layout="total,sizes,prev,pager,next,jumper"
      :total="total">
  </el-pagination>
</div>
</template>
<script>
import request from "@/utils/request";
import { Edit,Remove,CirclePlus,Upload,Download,Search,Refresh,View,CircleClose } from '@element-plus/icons-vue';
export default {
  setup(){
    return{
      Edit,
      Remove,
      CirclePlus,
      Upload,
      Download,
      Search,
      Refresh,
      View,
      CircleClose
    }
  },
  name: 'QuestionBank',
  components: {Edit},
  data() {
    return {
      form: {},
      dialogVisible: false,
      currentPage: 1,
      total: 10,
      pageSize: 10,
      garbageName:"",
      garbageType:"",
      tableData: [],
      bookList:[],
      bookVis:false,
      isShow:false,
      ids:[],
      types:[
        {
          typename:"干垃圾",
          typeid:1
        },
        {
          typename:"湿垃圾",
          typeid:2
        },
        {
          typename:"可回收垃圾",
          typeid:3
        },
        {
          typename:"有害垃圾",
          typeid:4
        },
      ]
    }
  },
  created() {
    this.load();
  },
  methods: {
    tableRowStyle({ row, rowIndex }) {
      return { background:'#F2F6FC', color: '#0052d9', 'font-weight': 600 }
    },
    reset(){
      this.garbageName="";
      this.garbageType="";

      this.load()
    },
    Show(){
      this.isShow=!this.isShow
    },
    deleteBatch(){
      if(!this.ids.length){
        this.$message.warning("请选择数据!")
        return
      }
      console.log(this.ids)
      request.post("/qbank/del/batch",this.ids).then(res=>{
        if(res){
          this.$message.success("批量删除成功")
          this.load()
        }else{
          this.$message.error("批量删除失败")
        }
      })
    },
    handleSelectionChange(val){
      console.log(val)
      this.ids=val.map(v => v.questionId)
    },
    handleUploadSuccess(res) {
      console.log(res)
      if (res.code === 200) {
        this.$message.success("导入成功")
        this.load()
      }
    },
    exportUser() {
      location.href = "http://localhost:8086/qbank/export";
    },

    showBooks(books){
      this.bookList=books;
      this.bookVis=true
    },

    load() {
      request.get("/qbank", {
        params: {
          pageNum: this.currentPage,
          pageSize: this.pageSize,
          garbageName:this.garbageName,
          garbageType:this.garbageType
        }
      }).then(res => {
        // console.log(res.code)
        this.tableData = res.records;
        this.total = res.total
      })

      // request.get("/role").then(res =>{
      //   this.roles=res.records;
      // })
    },
    add() {
      this.dialogVisible = true;
      this.form = {}
    },
    save() {
      if (this.form.questionId) {
        request.post("/qbank/add", this.form).then(res => {
          console.log(res);
          if (res.code === 200) {
            this.$message({
              type: "success",
              message: "更新成功"
            })
          } else {
            this.$message({
              type: "error",
              message: "更新失败"
            })
          }
          this.load();
          this.dialogVisible = false
        });
      } else {
        request.post("/qbank/add", this.form).then(res => {
          console.log(res);
          if (res.code === 200) {
            this.$message({
              type: "success",
              message: "新增成功"
            })
          } else {
            this.$message({
              type: "error",
              message: "新增失败"
            })
          }
          this.load();
          this.dialogVisible = false
        })
      }
    },
    handleEdit(row) {
      this.form = JSON.parse(JSON.stringify(row));
      this.dialogVisible = true
    },
    handleSizeChange(pageSize) {
      this.pageSize=pageSize;
      this.load();
    },
    handleCurrentChange(pageNum) {
      this.currentPage=pageNum;
      this.load();
    },
    handleDelete(id){

      request.delete("/qbank/"+id).then(res => {
        console.log(res.code)
        if (res){
          this.$message({
            type: "success",
            message: "删除成功"
          })
        }else {
          this.$message({
            type: "error",
            message: "删除失败"
          })
        }
        this.load()//重新加载
      });
    }
  }
}
</script>
